package com.ruoyi.stock.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stock.domain.StockInventoryDetail;
import com.ruoyi.stock.service.IStockInventoryDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.basedata.service.IBaseProductService;
import com.ruoyi.basedata.domain.BaseProduct;
import com.ruoyi.stock.service.IStockInventoryCheckService;
import com.ruoyi.stock.domain.StockInventoryCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 库存盘点明细Controller
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Controller
@RequestMapping("/stock/detail")
public class StockInventoryDetailController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(StockInventoryDetailController.class);

    private String prefix = "stock/detail";

    @Autowired
    private IStockInventoryDetailService stockInventoryDetailService;
    
    @Autowired
    private IBaseProductService baseProductService;
    
    @Autowired
    private IStockInventoryCheckService stockInventoryCheckService;

    @RequiresPermissions("stock:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /**
     * 查询库存盘点明细列表
     */
    @RequiresPermissions("stock:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StockInventoryDetail stockInventoryDetail)
    {
        startPage();
        List<StockInventoryDetail> list = stockInventoryDetailService.selectStockInventoryDetailList(stockInventoryDetail);
        return getDataTable(list);
    }

    /**
     * 导出库存盘点明细列表
     */
    @RequiresPermissions("stock:detail:export")
    @Log(title = "库存盘点明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StockInventoryDetail stockInventoryDetail)
    {
        List<StockInventoryDetail> list = stockInventoryDetailService.selectStockInventoryDetailList(stockInventoryDetail);
        ExcelUtil<StockInventoryDetail> util = new ExcelUtil<StockInventoryDetail>(StockInventoryDetail.class);
        return util.exportExcel(list, "库存盘点明细数据");
    }

    /**
     * 新增库存盘点明细
     */
    @RequiresPermissions("stock:detail:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存盘点明细
     */
    @RequiresPermissions("stock:detail:add")
    @Log(title = "库存盘点明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StockInventoryDetail stockInventoryDetail)
    {
        return toAjax(stockInventoryDetailService.insertStockInventoryDetail(stockInventoryDetail));
    }

    /**
     * 修改库存盘点明细
     */
    @RequiresPermissions("stock:detail:edit")
    @GetMapping("/edit/{detailId}")
    public String edit(@PathVariable("detailId") Long detailId, ModelMap mmap)
    {
        StockInventoryDetail stockInventoryDetail = stockInventoryDetailService.selectStockInventoryDetailByDetailId(detailId);
        mmap.put("stockInventoryDetail", stockInventoryDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存盘点明细
     */
    @RequiresPermissions("stock:detail:edit")
    @Log(title = "库存盘点明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StockInventoryDetail stockInventoryDetail)
    {
        return toAjax(stockInventoryDetailService.updateStockInventoryDetail(stockInventoryDetail));
    }

    /**
     * 删除库存盘点明细
     */
    @RequiresPermissions("stock:detail:remove")
    @Log(title = "库存盘点明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stockInventoryDetailService.deleteStockInventoryDetailByDetailIds(ids));
    }
    
    /**
     * 根据商品ID获取商品名称
     */
    @GetMapping("/getProductName/{productId}")
    @ResponseBody
    public AjaxResult getProductName(@PathVariable("productId") Long productId)
    {
        if (productId == null) {
            return AjaxResult.error("商品ID不能为空");
        }
        
        try {
            // 从基础数据模块获取商品信息
            BaseProduct baseProduct = baseProductService.selectBaseProductByProductId(productId);
            if (baseProduct != null && baseProduct.getProductName() != null) {
                return AjaxResult.success("获取成功", baseProduct.getProductName());
            } else {
                return AjaxResult.error("未找到对应的商品");
            }
        } catch (Exception e) {
            log.error("获取商品名称时发生错误", e);
            return AjaxResult.error("获取商品名称失败");
        }
    }
    
    /**
     * 根据盘点ID获取盘点单号
     */
    @GetMapping("/getCheckCode/{checkId}")
    @ResponseBody
    public AjaxResult getCheckCode(@PathVariable("checkId") Long checkId)
    {
        if (checkId == null) {
            return AjaxResult.error("盘点ID不能为空");
        }
        
        try {
            // 从盘点信息中获取盘点单号
            StockInventoryCheck check = stockInventoryCheckService.selectStockInventoryCheckByCheckId(checkId);
            if (check != null && check.getCheckCode() != null) {
                return AjaxResult.success("获取成功", check.getCheckCode());
            } else {
                return AjaxResult.error("未找到对应的盘点记录");
            }
        } catch (Exception e) {
            log.error("获取盘点单号时发生错误", e);
            return AjaxResult.error("获取盘点单号失败");
        }
    }
}
